package xyz.ljcc.ccmt.service.impl.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import xyz.ljcc.ccmt.commons.utils.UpdateTool;
import xyz.ljcc.ccmt.commons.utils.encode.EncodeUtils;
import xyz.ljcc.ccmt.model.base.User;
import xyz.ljcc.ccmt.model.base.UserVsDept;
import xyz.ljcc.ccmt.model.base.UserVsRole;
import xyz.ljcc.ccmt.repository.admin.UserRepository;
import xyz.ljcc.ccmt.repository.admin.UserVsDeptRepository;
import xyz.ljcc.ccmt.repository.admin.UserVsRoleRepository;
import xyz.ljcc.ccmt.service.api.admin.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	UserRepository userRepository;
	
	@Autowired
	UserVsRoleRepository userVsRoleRepository;
	
	@Autowired
	UserVsDeptRepository userVsDeptRepository;
	
	private static final String SALT = "lsjz"; // 盐值
	
	@Override
	public User getUser(String loginName, String password) {
		return userRepository.getByLoginNameAndPassword(loginName, EncodeUtils.md5(SALT + password));
	}

	@Override
	public List<User> loadUserList(String deptCode, String userName) {
		List<User> users = null;
		if (!StringUtils.isEmpty(userName)) { // 不为空,则根据用户名模糊匹配
			users = userRepository.getByUserNameContaining(userName);
		} else if (!StringUtils.isEmpty(deptCode)) { // 不为空，则根据科室号进行查询
			users = userRepository.getByDeptCode(deptCode);
		} else { // 否则，获取所有的用户信息
			users = userRepository.findAll();
		}
		return users;
	}

	@Override
	public User saveUser(User user) {
		user.setCreateTime(new Date());
		user.setActive(1); // 设置默认启用
		user.setPassword(EncodeUtils.md5(SALT + "a")); // 初始密码a
		user = userRepository.save(user);
		user.setLoginName(String.valueOf(user.getId() + 1000)); // 处理自动生成ID
		return userRepository.save(user);
	}

	@Override
	public User editUser(User user) {
		User user2 = userRepository.findOne(user.getId());
		UpdateTool.copyNullProperties(user2, user);
		return userRepository.save(user);
	}

	@Override
	public void delUser(Integer id) {
		// 删除用户角色
		userRepository.updateByHql(
				" delete from UserVsRole ur where ur.loginName in ( select u.loginName from User u where u.id = ?1 ) ",
				id);
		// 删除用户科室
		userRepository.updateByHql(
				"delete from UserVsDept ud where ud.loginName in ( select u.loginName from User u where u.id = ?1  )",
				id);
		// 删除用户
		userRepository.delete(id);
	}

	@Override
	public User getUser(Integer id) {
		return userRepository.findOne(id);
	}

	@Override
	public Map<String, Object> getPermission(String loginName) {
		Map<String, Object> map = new HashMap<>();
		List<UserVsRole> roleList = userVsRoleRepository.getByLoginName(loginName);
		List<UserVsDept> deptList = userVsDeptRepository.getByLoginName(loginName);
		map.put("roleList", roleList);
		map.put("deptList", deptList);
		return map;
	}

	@Override
	public void savePermission(String loginName, String roles, String depts) {
		List<UserVsRole> roleList = userVsRoleRepository.getByLoginName(loginName);
		List<UserVsDept> deptList = userVsDeptRepository.getByLoginName(loginName);
		List<String> roleStrList = new ArrayList<>();
		List<String> deptStrList = new ArrayList<>();
		// 删除
		for (UserVsDept userVsDept : deptList) { // 科室
			if (!depts.contains(userVsDept.getDeptCode() + ",")) { // 传入的里面不包含当前，表示这个不需要了
				userVsDeptRepository.delete(userVsDept);
			}
			deptStrList.add(userVsDept.getDeptCode());
		}
		for (UserVsRole userVsRole : roleList) { // 角色
			if (!roles.contains(userVsRole.getRoleId() + "id")) {
				userVsRoleRepository.delete(userVsRole);
			}
			roleStrList.add(userVsRole.getRoleId());
		}
		// 新增
		for (String roleId : roles.split(",")) {
			if (!roleStrList.contains(roleId)) {
				UserVsRole userVsRole = new UserVsRole();
				userVsRole.setRoleId(roleId);
				userVsRole.setLoginName(loginName);
				userVsRoleRepository.save(userVsRole);
			}
		}
		for (String deptCode : depts.split(",")) {
			if (!deptStrList.contains(deptCode)) {
				UserVsDept userVsDept = new UserVsDept();
				userVsDept.setLoginName(loginName);
				userVsDept.setDeptCode(deptCode);
				userVsDeptRepository.save(userVsDept);
			}
		}
	}

	@Override
	public void activeUser(Integer id, Integer active) {
		userRepository.updateByHql(" update User u set u.active = ?1 where u.id = ?2 ", active, id);
	}

	@Override
	public boolean modifyPwd(Integer id, String oldPassword, String newPassword) {
		User user = userRepository.findOne(id);
		if(EncodeUtils.md5(SALT + oldPassword).equals(user.getPassword())) { // 密码匹配
			User user2 = new User();
			UpdateTool.copyNullProperties(user, user2);
			user2.setPassword(EncodeUtils.md5(SALT + newPassword));
			userRepository.save(user2);
			return true;
		}else {
			return false;
		}
	}

	@Override
	public String getPassword(String password) {
		return EncodeUtils.md5(SALT + password);
	}

}
